package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.PageInfoToPageUtils;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ljj
 * @create 2022-11-17-15:25
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("成功");

    }

    @GetMapping("/page")
    public R<Page> save(int page, int pageSize, String name){
        PageHelper.startPage(page,pageSize);
        List<SetmealDto> a=setmealService.getSetmealList(name);
        PageInfo<SetmealDto> page1 = new PageInfo<>(a);
        Page<SetmealDto> tranto = PageInfoToPageUtils.tranto(page1);

        return R.success(tranto);

    }
    @DeleteMapping ("")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<String> ids){
        log.info("ids:{}",ids);

        setmealService.deletewWithDish(ids);


        return R.success("成功");
    }
    @DeleteMapping ("updatestatus")
    public R<String> updatestatus(@RequestParam List<String> ids){
        LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        setmealLambdaUpdateWrapper.set(Setmeal::getStatus,0).in(Setmeal::getId,ids);

        setmealService.update(setmealLambdaUpdateWrapper);



        return R.success("成功");
    }

    @GetMapping("/list")
    @Cacheable(value="setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list( Setmeal setmeal){
        LambdaUpdateWrapper<Setmeal> lambdaUpdateWrapper = new LambdaUpdateWrapper();
        lambdaUpdateWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lambdaUpdateWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lambdaUpdateWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(lambdaUpdateWrapper);


        return R.success(list);

    }
}
